import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt

# 生成一些示例数据
data = pd.DataFrame({
    'category': ['A', 'B', 'C', 'A', 'B', 'C'],
    'value': [10, 20, 15, 8, 12, 18]
})

# 侧边栏布局
st.sidebar.title("Settings")
# 在侧边栏添加一个滑动条用于选择数据筛选的阈值
threshold = st.sidebar.slider("Select a threshold value", 0, 20)

# 主体内容空间布局
st.title("Data Analysis Dashboard")
st.write("This is the main content area where we will display various visualizations and data based on the settings from the sidebar.")

# 容器布局
filtered_data_container = st.container()
with filtered_data_container:
    st.subheader("Filtered Data")
    # 根据侧边栏设置的阈值筛选数据
    filtered_data = data[data['value'] >= threshold]
    st.write(filtered_data)

# 列布局
col1, col2 = st.columns(2)
with col1:
    st.subheader("Bar Chart")
    # 绘制柱状图展示每个类别对应的数值（基于筛选后的数据）
    bar_chart_data = filtered_data.groupby('category')['value'].sum()
    plt.bar(bar_chart_data.index, bar_chart_data)
    st.pyplot(plt)

with col2:
    st.subheader("Line Chart")
    # 绘制折线图展示数值的变化趋势（基于筛选后的数据）
    line_chart_data = filtered_data.set_index('category')['value']
    plt.plot(line_chart_data.index, line_chart_data)
    st.pyplot(plt)